<template>
    <div class="descBox">
        <div class="fbc">
            <el-form :model="state.form" ref="queryRef" :inline="true" label-width="68px">
                <el-form-item label="模板类型" prop="configType" v-if="!templateTypeShow">
                    <div class="fac">
                        <el-select v-model="state.form.templateType" placeholder="请选择" clearable
                            @change="templateTypeChange">
                            <el-option v-for="dict in state.tempType" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </div>
                </el-form-item>
                <!-- <slot name="queryFrom"></slot> -->
                <el-form-item label="模板名称" prop="configType">
                    <el-input v-model="state.form.templateName" @blur="templateNameBlur" placeholder="请输入模板名称搜索"
                        clearable style="width: 240px">
                        <template #suffix>
                            <el-icon>
                                <search />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getTempListClick">查询</el-button>
                    <el-button @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
            <div class="btn_box">
                <span v-if="userStore.modify" :class="state.showAdd ? 'listInfoBtn active' : 'listInfoBtn'"
                    @click="addBtn">
                    <svg-icon :class="state.showAdd ? 'active' : ''" class-name="search-icon" icon-class="xinzeng" />
                    新增
                </span>
                <slot name="curdBtn"></slot>
                <div class="type_box" v-if="state.showAdd" @mouseleave="state.showAdd = false">
                    <div class="item" @click="openAdd('C')">新增分站模板</div>
                    <div class="item" @click="openAdd('D')">新增电源模板</div>
                    <div v-if="!templateTypeValue" class="item" @click="openAdd('A')">新增分析模板</div>
                    <div v-if="!templateTypeValue" class="item" @click="openAdd('B')">新增公式</div>
                </div>
            </div>
        </div>
        <Waterfall :list="state.mobanList" class="pets" :breakpoints="state.breakpoints" :key="state.mobanList">
            <template #default="{ item, url, index }">
                <div class="card_box" :class="{ type3: item.type == 'A', type4: item.type == 'B', 
                    type1: item.type == 'C',type2: item.type == 'D' }">
                    <p class="fbc">
                        <span>
                            <span v-if="item.type == 'A'" :class="{ type3: item.type == 'A' }">分析</span>
                            <span v-if="item.type == 'B'" :class="{ type4: item.type == 'B' }">公式</span>
                            <span v-if="item.type == 'C'" :class="{  type1: item.type == 'C' }">分站</span>
                            <span v-if="item.type == 'D'" :class="{type2: item.type == 'D'  }">电源</span>
                            <em v-if="item.type == 'A'">{{ item.name }}</em>
                            <em v-if="item.type == 'B'">{{ item.templateName }}</em>
                            <em v-if="item.type == 'C'">{{ item.templateName }}</em>
                            <em v-if="item.type == 'D'">{{ item.templateName }}</em>
                        </span>
                        <span style="display:flex;">
                            <slot name="curdBtnUse" :item="item"></slot>
                            <el-button v-if="userStore.modify" link type="primary" @click="editClick(item)">编辑
                            </el-button>
                        </span>
                    </p>
                    <div class="desc" v-if="item.type == 'A'">
                        <p>
                            <span>原因</span><span>{{ item.reason }}</span>
                        </p>
                        <p>
                            <span>处理措施</span><span>{{ item.measure }}</span>
                        </p>
                        <p>
                            <span>处理结果</span><span>{{ item.result }}</span>
                        </p>
                    </div>
                    <div class="desc" v-if="item.type == 'B'">
                        <p>
                            <span>类型</span><span>{{ item.outcome }}</span>
                        </p>
                        <p>
                            <span>公式</span><span>{{ item.expression }}</span>
                        </p>
                    </div>
                    <div class="desc" v-if="item.type == 'C'">
                        <p>
                            <span>测点</span><span>{{ item.nodeList.map(item=>{
                return item.featureId
              }).join('  ')}}</span>
                        </p>
                    </div>
                    <div class="desc" v-if="item.type == 'D'">
                        <p>
                            <span>电源类型</span><span>{{ item.powerTypeName }}</span>
                        </p>
                        <p>
                            <span>电源号</span><span>{{ item.powerNo==1?'一路':item.powerNo==2?'二路' :'三路'}}</span>
                        </p>
                        <p>
                            <span>测点</span><span>{{ item.powerTemplateList.map(item=>{
                return item.featureId
              }).join('  ')}}</span>
                        </p>
                    </div>
                </div>
            </template>
        </Waterfall>
        <!-- 使用模版 -->
        <el-dialog v-model="state.useTempValueShow" width="260px">
            <div class="del_box">
                <img src="@/assets/images/useTemp.png" alt="" />
                <div class="fonts">
                    <div class="one">是否将模板添加到{{state.useTempValue.item.type=='D'?'电源':'分站'}}？</div>
                    <div class="two">
                        【{{state.useTempValue.stationObj.stationCode}}】{{state.useTempValue.stationObj.stationPlaceName}}{{state.useTempValue.stationObj.stationTypeName}}
                    </div>
                </div>
                <div>
                    <el-button style="width: 72px; margin: 0 20px 0 0" @click="state.useTempValueShow = false">取消
                    </el-button>
                    <el-button style="width: 72px" type="primary" @click="confirmAddClick">添加</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
<script setup>
    import {
        computed,
        nextTick,
        reactive,
        watchEffect
    } from "vue";
    import axios from "@/utils/request";
    import dayjs from "dayjs";
    import {
        LazyImg,
        Waterfall
    } from "vue-waterfall-plugin-next";
    import "vue-waterfall-plugin-next/dist/style.css";
    import useUserStore from "@/store/modules/user";
    const {
        proxy
    } = getCurrentInstance();
    const userStore = useUserStore();
    const props = defineProps({
        delTemp: {
            type: Boolean,
            default: () => {
                return false;
            },
        },
        timetemp: {
            type: String,
            default: () => {
                return undefined;
            },
        },
        templateType: {
            type: String,
            default: () => {
                return undefined;
            },
        },
        templateTypeValue: {
            type: String,
            default: () => {
                return undefined;
            },
        },
        delTempmobanType: {
            type: String,
            default: () => {
                return undefined;
            },
        },
        useTempValue: {
            type: String,
            default: () => {
                return undefined;
            },
        },
        templateTypeShow: {
            type: Boolean,
            default: () => {
                return false;
            },
        },
     
    });
    const emits = defineEmits(['update:templateTypeValue'])
    let state = reactive({
        templateTypeChangeShow: false,
        useTempValueShow: false,
        useTempValue: undefined,
        mobanType: 'C',
        queryParams: {}, //搜索参数
        breakpoints: {
            1920: {
                //当屏幕宽度小于等于1200
                rowPerView: 3,
            },
            1200: {
                //当屏幕宽度小于等于1200
                rowPerView: 4,
            },
            800: {
                //当屏幕宽度小于等于800
                rowPerView: 2,
            },
            500: {
                //当屏幕宽度小于等于500
                rowPerView: 2,
            },
        },
        mobanList: [],
        form: {},
        showAdd: false,
        editId: undefined,
        tempType: [{
                label: "分析模板",
                value: "A",
            },
            {
                label: "公式",
                value: "B",
            },
            {
                label: "分站模板",
                value: "C",
            },
            {
                label: "电源模板",
                value: "D",
            },
        ],
        timetemp: true,
        templateName: undefined
    })
    const getTempList = () => {
        axios.post("template/expression/obtainAll", state.form).then((res) => {
            state.mobanList = res.data;
        });
    };
    // 新增按钮
    function addBtn(row) {
        state.showAdd = true;
        state.editId = undefined;
    }

    function openAdd(n) {
        state.useTempValueShow = false
        emits('openAdd', n)
    }

    function editClick(n) {
        state.useTempValueShow = false
        emits('editClick', n)
    }

    function getTempListClick() {
        getTempList();
    }

    function reset() {
        state.useTempValueShow = false
        state.form.templateType = props.templateTypeValue
        emits('reset', {})
        state.form = {}
        if (!props.templateTypeValue) {
            getTempList();
        }
    }

    function templateNameBlur(val) {
        console.log(val, "state.templateName");
    }

    function templateTypeChange(val) {
        state.templateTypeChangeShow = true
    }

    function confirmAddClick() {
        let url = 'point/useStationTemplate'
        if (state.useTempValue.item.type == 'D') {
            url = 'power/usePowerTemplate'
        } else {
            url = 'point/useStationTemplate'
        }
        axios.get(
            `/${url}?stationCode=${state.useTempValue.stationObj.stationCode}&templateName=${state.useTempValue.item.templateName}`
        ).then(res => {
            state.useTempValueShow = false
            emits('confirmAddClick', true)
        }).catch(err => {
            state.useTempValueShow = false
            emits('confirmAddClick', true)
        })
    }
    onMounted(() => {
        // state.form.templateType = props.templateTypeValue==0?'C,D':state.form.templateType
        // getTempList()
    });
    watch(() => {
        if (props.templateTypeValue) {
            state.mobanList = []
            state.form.templateType = props.templateTypeValue
            getTempList()
            state.useTempValueShow = false
        } 
         if (props.delTemp) {
            state.form.templateType = props.delTempmobanType
            getTempList()
        }else{
             if (!state.templateTypeChangeShow) {
                getTempList()
            }
        }
        if (props.useTempValue) {
            state.useTempValueShow = true
            // }
            state.useTempValue = props.useTempValue
        }
    })
</script>
<style lang="scss" scoped>
    .del_box {
        padding: 0 20px;
        height: 235px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;

        img {
            width: 100px;
        }

        .fonts {
            color: rgba(62, 70, 79, 1);
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;

            .one {
                font-size: 16px;
                font-weight: 700;
            }

            .two {
                font-size: 14px;
                font-weight: 400;
            }
        }
    }

    .card_box {
        padding: 10px 16px 16px;
        border-radius: 4px;

        &.type1 {
            background-color: rgba(10, 170, 220, 0.08);
        }

        &.type11 {
            background-color: rgba(219, 243, 253, 0.8);
        }

        &.type2 {
            background-color: rgba(47, 173, 104, 0.08);
        }

        &.type22 {
            background-color: rgba(222, 243, 243, 0.8);
        }

        &.type3 {
            background-color: rgba(171, 122, 224, 0.08);
        }

        &.type33 {
            background-color: rgba(232, 239, 253, 0.8);
        }

        &.type4 {
            background-color: rgba(71, 137, 230, 0.08);
        }

        &.type44 {
            background-color: rgba(224, 240, 253, 0.8);
        }

        .fbc {
            span {
                font-size: 12px;
                border-radius: 2px;
                padding: 4px;

                &.type1 {
                    background-color: rgba(10, 170, 220, 0.16);
                    color: rgba(10, 170, 220, 1);
                }

                &.type2 {
                    background-color: rgba(47, 173, 104, 0.16);
                    color: rgba(47, 173, 104, 1);
                }

                &.type3 {
                    background-color: rgba(171, 122, 224, 0.16);
                    color: rgba(171, 122, 224, 1);
                }

                &.type4 {
                    background-color: rgba(71, 137, 230, 0.16);
                    color: rgba(71, 137, 230, 1);
                }
            }

            em {
                font-style: normal;
                font-size: 14px;
                font-weight: 700;
                margin-left: 8px;
            }
        }

        .desc {
            border-radius: 4px;
            background: rgba(255, 255, 255, .8);
            padding: 16px;
            font-size: 14px;

            p {
                color: rgba(62, 70, 79, 1) !important;

                span {
                    display: inline-block;

                    &:nth-child(1) {
                        min-width: 60px;
                        color: rgba(62, 70, 79, 0.6) !important;
                    }

                    &.use {
                        color: rgba(64, 158, 255, 1);
                        border-bottom: 1px solid rgba(64, 158, 255, 1);
                    }
                }
            }
        }
    }

    .pets {
        width: 100% !important;
        margin: 0 auto;
        display: flex;
        // flex-wrap: wrap;
        background-color: transparent;
    }

    .btn_box {
        position: relative;
        display: flex;

        .type_box {
            position: absolute;
            top: 32px;
            left: -15px;
            z-index: 9;
            padding: 4px 0;
            width: 116px;
            height: 136px;
            background-color: #fff;

            .item {
                width: 100%;
                height: 32px;
                font-size: 14px;
                color: rgba(62, 70, 79, 1);
                display: flex;
                align-items: center;
                justify-content: center;
            }

            .item:hover {
                cursor: pointer;
                background-color: #f0f3f4;
            }
        }
    }
</style>